import { defineStore } from "pinia";
import Cookies from "js-cookie";

export const useUserStore = defineStore("user", () => {
    let tokenVal = ref("");
    const token = computed({
        get() {
            return tokenVal.value;
        },
        set(value) {
            if (process.client) {
                if (!value) {
                    if (process.env.NODE_ENV === "development") {
                        Cookies.remove("ykj_token", { path: "/", domain: "localhost" });
                    } else {
                        Cookies.remove("ykj_token", { path: "/", domain: ".ykjhr.com" });
                    }
                } else {
                    Cookies.set("ykj_token", value, { expires: 7, path: "/", domain: process.env.NODE_ENV === "development" ? "localhost" : ".ykjhr.com" });
                }
            }
            tokenVal.value = value;
        },
    });
    const userInfo = ref({});
    const loginInfo = useCookie("loginInfo", {
        default: () => ({}),
        maxAge: 60 * 60 * 24 * 30,
    });

    const avatar = ref("");
    const isLogin = computed(() => {
        return !!tokenVal.value;
    });

    const midNum = ref({
        like_num: 0,
        star_num: 0,
        score_num: 0,
        case_like_num: 0,
        case_star_num: 0,
    });

    const scoreLog = ref([]);

    const messageLog = ref({});

    const SET_LOGININFO = (v) => {
        loginInfo.value = v;
    };

    const CLEAR_USERINFO = () => {
        userInfo.value = {};
        messageLog.value = {};
        token.value = "";
        avatar.value = "";
        midNum.value = {
            like_num: 0,
            star_num: 0,
            score_num: 0,
            case_like_num: 0,
            case_star_num: 0,
        };
    };

    return {
        userInfo,
        token,
        loginInfo,
        avatar,
        isLogin,
        midNum,
        scoreLog,
        messageLog,
        SET_LOGININFO,
        CLEAR_USERINFO,
    };
});

export const useDialogStore = defineStore("dialog", () => {
    const isPayDialogVisible = ref(false);
    const isDetailDialogVisible = ref(false);
    const isOrderStatusDialogVisible = ref(false);
    const isBuyTemplateProtocolDialog = ref(false);
    const isReleaseNoteDialog = ref(false);
    const isShareTemplateDialogVisible = ref(false);
    const isLoginTipsDialogVisible = ref(false);
    const isCommentListDialogVisible = ref(false);
    const isCaseCommentListDialogVisible = ref(false);
    const isMidListDialogVisible = ref(false);
    const isTaskListDialogVisible = ref(false);
    const isScoreSuccessMsgVisible = ref(false);
    const isSignInDialogVisible = ref(false);
    const isScoreLotteryDialogVisible = ref(false);
    const isScoreLotterySuccessDialogVisible = ref(false);
    const isScoreLotteryFailDialogVisible = ref(false);
    const isEditorUpdateTipsDialogVisible = ref(false);
    const isNoticeTipsDialogVisible = ref(false);
    const isSearchDialogVisible = ref(true);
    const isSzrVisible = ref(false);
    const loginCancelBtn = ref(true);
    const shareUrl = ref("");
    const shareName = ref("");
    const shareTitle = ref("");
    const commentWorkId = ref("");
    const commentCaseId = ref("");
    const midListType = ref(0);
    const scoreSuccessNum = ref(0);
    const scoreSuccessDialogState = ref({});

    return {
        isPayDialogVisible,
        isDetailDialogVisible,
        isOrderStatusDialogVisible,
        isBuyTemplateProtocolDialog,
        isShareTemplateDialogVisible,
        isLoginTipsDialogVisible,
        isCaseCommentListDialogVisible,
        isCommentListDialogVisible,
        isMidListDialogVisible,
        isTaskListDialogVisible,
        isScoreSuccessMsgVisible,
        isSignInDialogVisible,
        isScoreLotteryDialogVisible,
        isScoreLotterySuccessDialogVisible,
        isScoreLotteryFailDialogVisible,
        isEditorUpdateTipsDialogVisible,
        isReleaseNoteDialog,
        isNoticeTipsDialogVisible,
        isSearchDialogVisible,
        isSzrVisible,
        loginCancelBtn,
        shareUrl,
        shareName,
        shareTitle,
        commentWorkId,
        commentCaseId,
        midListType,
        scoreSuccessNum,
        scoreSuccessDialogState,
    };
});

export const useOrderStore = defineStore("order", {
    state: () => ({
        order_type: 1, // 1 vip, 2 模版
        buy_type: "", // 购买类型
        temspecs_type: "year", // 购买单位
        authority: [], // 权限详情
        temspecs_id: "", // 购买类型ID
        order_price: "",
        vip_type: "", // 会员等级
        order_no: "",
        template_id: "", // 模版ID
    }),
});

export const usePageStore = defineStore("page", () => {
    const pageName = ref("");
    const loginBackUrl = ref("");
    const urlParams = ref({});
    const scoreParams = ref({});
    const baiduCustomer = ref(false);
    const isMobile = ref(false);
    const isLoginOpen = ref(false);
    const isIe = ref(false);
    const isVideoError = ref(false);
    const onConfirm = ref(undefined);

    const residenceTime = ref({});

    return {
        pageName,
        loginBackUrl,
        baiduCustomer,
        isMobile,
        urlParams,
        scoreParams,
        isLoginOpen,
        isIe,
        isVideoError,
        residenceTime,
        onConfirm
    };
});

export const useTemplate = defineStore('template', () => {
    const userStore = useUserStore();
    const type = [
        { id: 1, name: "展示模板", code: "template", all: true, value: 0 },
        { id: 2, name: "课件模板", code: "courseware", all: true, value: 1 },
        { id: 8, name: "数据模版", code: "shuju", all: true, value: 2 },
        { id: 11, name: "自定义模版", code: "zhonghe", all: true, value: 3 },
    ];

    const typeList = computed(() => {
        return type.filter((item) => item.all || userStore.userInfo.level == 3);
    })

    return {
        typeList,
    }
})


export const useCacheStore = defineStore("cacheKey", () => {
    const key = ref(String(Date.now()));

    const setKey = (val) => {
        key.value = val;
    }

    return {
        key,

        setKey
    }
})